package com.blueocean_health.care.management.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;

import com.blueocean_health.care.management.domain.base.OrderServiceInfo;
import com.blueocean_health.care.management.util.MyMapper;

@Mapper
public interface OrderServiceInfoMapper extends MyMapper<OrderServiceInfo> {

    @Select("SELECT *FROM order_service_info WHERE order_id =#{orderId} AND version =#{version} AND stflag = 1 AND cancel = 0 AND order_type != 3 ORDER BY begin_time LIMIT 1")
    public OrderServiceInfo getFirstByOrderIdAndVersion(@Param("orderId") String orderId, @Param("version") Long version);

    @Select("SELECT *FROM order_service_info WHERE order_id =#{orderId} AND version =#{version} AND stflag = 1 AND cancel = 0 AND order_type != 3 ORDER BY end_time DESC LIMIT 1")
    public OrderServiceInfo getLastByOrderIdAndVersion(@Param("orderId") String orderId, @Param("version") Long version);

    @Select("SELECT *FROM order_service_info WHERE order_id =#{orderId} AND version =#{version} AND stflag = 1 AND cancel = 0 ")
    public List<OrderServiceInfo> getListByOrderIdAndVersion(@Param("orderId") String orderId, @Param("version") Long version);


    @Select("SELECT * from 	(SELECT * FROM order_service_info b WHERE version = (SELECT max(version) FROM order_service_info "
    		+ "WHERE b.service_id = service_id)) a WHERE a.order_id = #{orderId} and cancel=0")
    public List<OrderServiceInfo> getOrderServiceByOrderId(@Param("orderId") String orderId);
    @Select("SELECT * from 	(SELECT * FROM order_service_info b WHERE version = (SELECT max(version) FROM order_service_info "
    		+ "WHERE b.order_id = order_id)) a WHERE a.order_id = #{orderId} and cancel=0")
    public List<OrderServiceInfo> getOrderServiceByOrderIdNew(@Param("orderId") String orderId);
    @Select("SELECT * from 	(SELECT * FROM order_service_info b WHERE version = (SELECT max(version) FROM order_service_info "
    		+ "WHERE b.service_id = service_id)and version=	(SELECT max(version) FROM order_info where order_id=#{orderId})) a"
    		+ " WHERE a.order_id = #{orderId} and a.begin_time<#{refundTime}  and a.end_time>#{refundTime}")
    public List<OrderServiceInfo> getOrderServiceByOrderIdAndRefundTime(@Param("orderId") String orderId, @Param("refundTime") String refundTime);
    
    @Select("SELECT * from 	(SELECT * FROM order_service_info b WHERE version = (SELECT max(version) FROM order_service_info "
    		+ "WHERE b.service_id = service_id)and version=	(SELECT max(version) FROM order_info where order_id=#{orderId})) "
    		+ "a WHERE a.order_id = #{orderId} and a.begin_time>=#{refundTime} ")
    public List<OrderServiceInfo> getOrderServiceByOrderIdAndEnd(@Param("orderId") String orderId, @Param("refundTime") String refundTime);
    @Select("SELECT * from 	(SELECT * FROM order_service_info b WHERE version = (SELECT max(version) FROM order_service_info "
    		+ "WHERE b.service_id = service_id)and version=	(SELECT max(version) FROM order_info where order_id=#{orderId} )) a "
    		+ "WHERE a.order_id = #{orderId} and a.end_time<=#{refundTime} ")
    public List<OrderServiceInfo> getOrderServiceByOrderIdAndStart(@Param("orderId") String orderId, @Param("refundTime") String refundTime);
    
    @Update("UPDATE order_service_info SET cancel=1 WHERE order_id=#{orderId}")
	public void deleteOrder(@Param("orderId")String orderId);

    @Update("UPDATE order_service_info SET service_refund_flag =#{serviceRefundFlag} WHERE service_id=#{serviceId}")
    public int updateServiceRefundFlagByServiceId(@Param("serviceId")String serviceId,@Param("serviceRefundFlag")String serviceRefundFlag);

    @Update("UPDATE order_service_info SET service_refund_flag =#{serviceRefundFlag} WHERE order_id=#{orderId}")
    public int updateServiceRefundFlagByOrderId(@Param("orderId")String orderId,@Param("serviceRefundFlag")String serviceRefundFlag);

    @Select("SELECT *FROM order_service_info WHERE service_id =#{ServiceId}")
    public OrderServiceInfo getOrderServiceInfoByServiceId(@Param("ServiceId") String ServiceId);

    /**
     * @Description: 删除老版本号的数据
     * @param:入参  orderid  version
     * @return:出参
     * @throws:
     */
    @Delete("DELETE FROM order_service_info WHERE order_id =#{orderId} AND VERSION !=#{version}")
    int deleteOldVersionOrderServiceByOrderId(@Param("orderId")String orderId,@Param("version") Long version );

    /**
    * @Description: 更换护工时，物理删除所关联的退款单
    * @param:入参
    * @return:出参
    * @throws:
    */
    @Delete("DELETE FROM order_service_info WHERE refund_service_id =#{orderServiceId} ")
    int deleteRundOrderService(@Param("orderServiceId")String orderServiceId );

    /**
     * @Description: 更换护工时，物理删除原订单明细
     * @param:入参
     * @return:出参
     * @throws:
     */
    @Delete("DELETE FROM order_service_info WHERE service_id =#{orderServiceId} ")
    int deleteOrderService(@Param("orderServiceId")String orderServiceId );



    /**
     * 根据楼号楼层科室Id 查询是否绑定有订单  2018年1月16日11:08:07
     * @param map
     * @return
     */
    List<String> getOrderDeptFlag(Map<String,String> map);
}